// pages/tuijian/index.js
const app = getApp();
let globalData = getApp().globalData;
const banklist = require('../../utils/banklist.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    windowHeight:'',
    user_id:'',//经纪人id
    quan:false,//业主显示优惠券
    quan_read:false,
    identity:'',//身份
    qian_img: globalData.qian_img,
    quan_arr:{},
    tjcurrentId: '0',
    tj_status: [ {
      name: '审核中',
      num: '1',
      typeId: '1'
    }, {
      name: '接待中',
      num: '1',
      typeId: '2'
    }],
    project: [{
      name: '众诚龙城天御',
      pid: '34'
    }, {
      name: '至尊府',
      pid: '35'
    }],
    guwen: [],
    huxing: [],
    project_index: 0,//默认显示位置
    huxing_index: 0,//默认显示位置
    guwen_index: 0,//默认显示位置
    wait_list:[],//审核中数据
    pass_list:[],//接待中数据
    resendTime: 60,//验证码重新发送时间 要与verificationTime保持一致
    verification: '', //验证码
    verificationText: '发送',
    verificationTime: 60,
    sending: false,
    name:'', //客户姓名
    tel:'', //客户电话
    project_sel:'',//选择项目
    huxing_sel:'',//选择户型
    guwen_id:'0',//置业顾问id
    desc:'', //购房需求
    tuijian_read: false, // 推荐须知
    jiangli_read: false, // 奖励政策
    lianma_read: false, // 这是什么
    fuli_read: false, //我的注册礼
    inputValue: null,
    quan_ling: false,//去领取
    quan_use: false,//使用
    quan_used: false,//已使用
    quan_qrcode: false,//优惠券核销
    quan_name:'',//优惠券的名字
    qrcode_url:'',//核销码
    red_code:'',//红包码
    invite_qrcode:'',//邀请二维码
    invite_code: '',//邀请码
    isbank_card:true, //是否填写银行卡
    bank_card:'必填',
    bank_name: '',
  },
  //推荐状态
  tuijianTap: function (e) {
    let id = e.currentTarget.id;
    let user_id = this.data.user_id;
    console.log(id);
    this.setData({
      tjcurrentId: id
    })
    if (id==1) {
      this.getWaitLocal(user_id);
      this.getNum(user_id);
    }else if(id==2){
      this.getPassLocal(user_id);
      this.getNum(user_id);
    }
  },
  tapInput() {
    this.setData({
      //在真机上将焦点给input
      inputFocus: true,
      //初始占位清空
      bank_card: ''
    });
  },
  //确认推荐
  cardSubmit: function () {
    var user_id = this.data.user_id;
    this.submitCard(user_id);
  },
  //我的注册礼
  tofuli: function (e) {
    this.setData({
      fuli_read: true
    })
  },
  //推荐须知
  toxuzhi:function(e){
    this.setData({
      tuijian_read: true
    })
  },
  //奖励政策
  tojiangli: function (e) {
    this.setData({
      jiangli_read: true
    })
  },
  //优惠券
  toquan: function (e) {
    this.setData({
      quan_read: true
    })
  },
  //这是什么
  tolianma: function (e) {
    this.setData({
      lianma_read: true,
    })
  },
  //邀请ta
  toyaoqing: function (e) {
    let user_id = this.data.user_id;
    let invite_qrcode = this.data.invite_qrcode;
    let invite_code = this.data.invite_code;
    console.log(user_id);
    wx.navigateTo({
      url: '../share_agent/index?user_id=' + user_id + '&invite_qrcode=' + invite_qrcode + '&invite_code=' + invite_code,
    })
  },
  // 关闭推荐须知/奖励政策
  closeModel() {
    console.log('关闭')
    this.setData({
      tuijian_read: false,
      jiangli_read: false,
      lianma_read: false,
      quan_read: false,
      fuli_read: false,
      isbank_card:false
    })
    console.log(this.data.quan_read);

  },
  //关闭核销
  closeModel_qrcode(){
    this.setData({
      quan_qrcode: false
    })
  },
  // input输入事件
  inputChange(event) {
    let key = event.currentTarget.dataset.key;
    let value = event.detail.value;
    this.setData({
      [key]: value
    })
    if(key == 'bank_card'){
      this.setData({
        bank_card: value
      })
    }
  },

  //input清空
  clearInputEvent: function (res) {
    this.setData({
      inputValue: ''
    })
  },
  //input失去焦点事件
  bindblur(event) {
    var _this = this;
    let key = event.currentTarget.dataset.key;
    let value = event.detail.value;
    _this.setData({
      [key]: value,
      bank_card: value
    });
    if (!/^\d{16,19}$/g.test(value)) {
      getApp().globalData.showHint('银行卡号错误');
      return false;
    } else {
      wx.request({
        url: 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json', //仅为示例，并非真实的接口地址
        data: {
          cardNo: value,
          cardBinCheck: 'true'
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res)
          console.log(res.data.bank)
          _this.setData({
            bank_name: banklist.dataList[res.data.bank]
          })
        }
      })
    }
  },
  //意向楼盘
  changeProject(e) {
    this.setData({
      project_index: e.detail.value,
      project_sel: this.data.project[e.detail.value].pid
    })
    this.getGuwen();
    this.getHouse();
  },
  //意向户型
  changeHuxing(e){
    this.setData({
      huxing_index: e.detail.value,
      huxing_sel: this.data.huxing[e.detail.value]
    })
  },
  //置业顾问
  changeGuwen(e) {
    this.setData({
      guwen_index: e.detail.value,
      guwen_id: this.data.guwen[e.detail.value].id
    })
  },
  // 获取验证码
  getVerification() {
    let _this = this;
    if (this.data.sending) {
      console.log('正在获取中');
      return false;
    }
    if (this.data.tel && (/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.tel))) {
      let time = this.data.verificationTime;
      let data = {
        pid: this.data.pid,
        mobile: this.data.tel,
        type: 'recommend'
      }
      globalData.sendPost('captcha/send', data).then(res => {
        if (res.status == 1) {
          globalData.showHint(res.message);
        } else {
          globalData.showHint(res.message);
        }
      })
      let timer = setInterval(function () {
        time--;
        if (time == 0) {
          clearInterval(timer);
          _this.setData({
            verificationText: '发送',
            verificationTime: _this.data.resendTime,
            sending: false
          })
        } else {
          _this.setData({
            verificationText: time + 's',
            verificationTime: time,
            sending: true
          })
        }
      }, 1000)
    } else {
      wx.showToast({
        title: '请填写正确电话',
        icon: 'none'
      })
    }
  },
  //确认推荐
  tuijianSubmit:function(){
    this.tuijianSubmit();
  },
  //我的注册礼/领取福利
  tofuli: function (e) {
    let user_id = this.data.user_id;
    let type = e.currentTarget.dataset.type;
    this.getRedcode(user_id, type);
    this.setData({
      fuli_read: true
    })
  },
  //去领取
  tolingqu:function(e){
    let user_id = this.data.user_id;
    let coupons_id = e.currentTarget.dataset.id;
    this.lingquQuan(user_id, coupons_id);
  },
  //使用
  toshiyong: function (e){
    let coupons_qrcode = e.currentTarget.dataset.qrcode;
    let coupons_name = e.currentTarget.dataset.name;
    this.setData({
      quan_qrcode:true,
      quan_name: coupons_name,
      qrcode_url: coupons_qrcode
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.user_id);
    console.log(options.invite_qrcode);
    console.log(options.invite_code);
    console.log(options.identity);
    let project = this.data.project;
    this.setData({
      project_sel: project[0].pid,
      windowHeight: wx.getSystemInfoSync().windowHeight,
      user_id: options.user_id,
      identity: options.identity,
      invite_code: options.invite_code,
      invite_qrcode: options.invite_qrcode
    })
    if (this.data.identity == '业主'){
      this.setData({
        quan: true,
      });
    }
    this.getHouse();
    this.getGuwen();
    this.getNum(options.user_id);
    this.getQuan(options.user_id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onUnload: function () {
    wx.redirectTo({
      url: 'pages/index/index',//指定界面
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取头部数量
  getNum(e){
    let data = {
      agent_id: e,
    }
    globalData.sendPost('custom/num', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          ['tj_status[0].num']: res.data.waitNum,
          ['tj_status[1].num']: res.data.passNum,
        })
        if(res.have_bank == 1){
          this.setData({
            isbank_card:false
          })
        }else{
          this.setData({
            isbank_card: true
          })
        }
      }
    })
  },
  //提交银行卡
  submitCard(e) {
    if (!this.data.bank_card) {
      getApp().globalData.showHint('银行卡号错误');
      return false;
    }
    let data = {
      cid: e,
      bank_name: this.data.bank_name,
      bank_card: this.data.bank_card
    }
    console.log(data);
    globalData.sendPost('custom/add-bank', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          isbank_card:false
        })
      }
    })
  },
  //经纪人-获取红包码
  getRedcode(e, type) {
    let data = {
      cid: e,
      type: type
    }
    globalData.sendPost('custom/red-code', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          red_code: res.code,
        })
      }
    })
  },
  //获取优惠券数据
  getQuan(e){
    let data = {
      cid: e,
    }
    globalData.sendPost('custom/coupons-info', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          quan_arr:res.coupons
        })
      }
    })
  },
  //领取优惠券
  lingquQuan(e,id) {
    let data = {
      cid: e,
      coupons_id: id
    }
    globalData.sendPost('custom/get-coupons', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000,
        });
        this.getQuan(e);
      }
    })
  },
  //获取审核中数据
  getWaitLocal(e) {
    let data = {
      agent_id: e,
    }
    globalData.sendPost('custom/check-list', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          wait_list:res.data
        })
      }
    })
  },
  //获取接待中数据
  getPassLocal(e) {
    let data = {
      agent_id: e,
    }
    globalData.sendPost('custom/pass-list', data).then(res => {
      if (res.status == 1) {
        console.log(res);
        this.setData({
          pass_list: res.data
        })
      }
    })
  },
  //获取户型列表数据
  getHouse() {
    let data = {
      pid: this.data.project_sel,
    }
    console.log(data);
    globalData.sendPost('custom/house', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          huxing: res.data,
          huxing_sel:res.data[0]
        })
      }
    })
  },
  //获取顾问列表数据
  getGuwen() {
    let data = {
      pid: this.data.project_sel,
    }
    globalData.sendPost('project/adviser-list', data).then(res => {
      console.log(res)
      if (res.status == 1) {
        this.setData({
          guwen: res.data
        })
        var guwen = this.data.guwen;
        var addguwen = {id:"0",name:"随机分配"};
        guwen.unshift(addguwen)
        this.setData({
          guwen: guwen
        })
      }
    })
  },
  //推荐客户
  tuijianSubmit() {
    if (!this.data.name) {
      getApp().globalData.showHint('请输入客户姓名');
      return false;
    }
    if (!this.data.tel || (!/^1[3|5|6|7|8|9]\d{9}$/.test(this.data.tel))) {
      getApp().globalData.showHint('电话错误');
      return false;
    }
    // if (!this.data.verification) {
    //   getApp().globalData.showHint('验证码不能为空');
    //   return false;
    // }
    let data = {
      pid:this.data.project_sel,
      agent_id: this.data.user_id,
      name: this.data.name,
      mobile: this.data.tel,
      captcha: this.data.verification,
      house: this.data.huxing_sel,
      aid: this.data.guwen_id,
      desc:this.data.desc,
    }
    globalData.sendPost('custom/recommend', data).then(res => {
      if (res.status == 1) {
        wx.showToast({
          title: '推荐成功',
          icon: 'success',
          duration: 2000,
        });
        this.clearInputEvent();
        var user_id = this.data.user_id;
        this.getNum(user_id);
        this.setData({
          huxing_index: 0,//默认显示位置
          guwen_index: 0,//默认显示位置
        })
      } else {
        globalData.showHint(res.msg);
        return false;
      }
    })
    return true;
  },

})